<html lang="en">
<head>
    <title>员工管理</title>
    <#include "../common/header.ftl">
    <script>
        $(function () {
            // 添加/编辑
            $('.editBtn').click(function () {
                $("#editForm")[0].reset();
                var data = $(this).data("json");
                if(data){
                    $('#editModal input[name="id"]').val(data.id);
                    $('#editModal input[name="name"]').val(data.name);
                    $('#editModal input[name="password"]').val(data.password);
                    $('#editModal input[name="email"]').val(data.email);
                    $('#editModal input[name="age"]').val(data.age);
                    $('#editModal select[name="admin"]').val(data.admin + '');
                    $('#editModal input[name="_method"]').val('PUT');
                }else{
                    $('#editModal input[name="_method"]').val('');
                }
                $('#editModal').modal('show');
            });
            $('.submitBtn').click(function () {
                $('#editForm').ajaxSubmit(function (data) {
                    if(data.success){
                        window.location.href = '/employees';
                    }else{
                        $.messager.alert("温馨提示", "操作失败");
                    }
                })
            });
            // 删除
            $('.deleteBtn').click(function () {
                var employeeId = $(this).data('id');
                $.messager.confirm("温馨提示", "你确定要删除吗?", function () {
                    $.ajax({
                        type:"delete",
                        url: "/employees/"+employeeId,
                        success: function(data){
                            if(data.success){
                                window.location.href = '/employees';
                            }else{
                                $.messager.alert("温馨提示", "删除失败");
                            }
                        }
                    });
                });
            });
        });
    </script>
</head>
<body>
<!--左侧菜单回显变量设置-->
<#assign  currentMenu="employee">
<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-10">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">员工管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/employees" method="get">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label for="keyword">关键字:</label>
                    <input type="text" class="form-control" id="keyword" name="keyword" value="${(qo.keyword)!}" placeholder="请输入姓名/邮箱">
                </div>
                <button class="btn btn-info"><span class="glyphicon glyphicon-search"></span> 查询</button>
                <button type="button" class="btn btn-info editBtn"><span class="glyphicon glyphicon-plus"></span> 添加</button>
            </form>

            <table class="table table-striped table-hover" >
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>名称</th>
                        <th>email</th>
                        <th>年龄</th>
                        <th>部门</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <#list result as entity>
                   <tr>
                       <td>${entity_index + 1}</td>
                       <td>${(entity.name)!}</td>
                       <td>${(entity.email)!}</td>
                       <td>${(entity.age)!}</td>
                       <td>${(entity.dept.name)!}</td>
                       <td>
                           <a class="btn btn-info btn-xs editBtn"  data-json='${entity.jsonString}'>
                               <span class="glyphicon glyphicon-pencil"></span> 编辑
                           </a>
                           <a href="javascript:;"  data-id="${entity.id!}" class="btn btn-danger btn-xs deleteBtn" >
                               <span class="glyphicon glyphicon-trash"></span> 删除
                           </a>
                       </td>
                   </tr>
                </#list>
            </table>
        </div>
    </div>
</div>
<div class="modal fade" id="editModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span> </button>
                <h4 class="modal-title">员工编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/employees" method="post" id="editForm">
                    <input type="hidden" value="" name="id">
                    <input type="hidden" name="_method" value=""/>
                    <div class="form-group" >
                        <label  class="col-sm-3 control-label">员工姓名：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name" value="" placeholder="请输入员工姓名">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-3 control-label">员工密码：</label>
                        <div class="col-sm-6">
                            <input type="password" class="form-control"  name="password" value="" placeholder="请输入员工密码">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-3 control-label">员工邮箱：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control"  name="email" value="" placeholder="请输入员工邮箱">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-3 control-label">员工年龄：</label>
                        <div class="col-sm-6">
                            <input type="number" class="form-control"  name="age" value="" placeholder="请输入员工年龄">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-3 control-label">是否是管理员：</label>
                        <div class="col-sm-6">
                            <select name="admin" class="form-control">
                                <option value="false">否</option>
                                <option value="true">是</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary submitBtn" >保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal" >取消</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>